/**
 * Created by liubin on 2017/5/10.
 */


var DzpkTableChatInfoLayer = GameCCSBase.extend({

    mChatUIs : null,

    mPlayerPoss : null,

    ctor: function () {
        this._super();
        this.loadCocostudio("res/ccs/DzpkChatLayer.json");

        //聊天
        this.mChatUIs = [];
        for (var i = 0; i < 5; i++){
            var Panel_chat = mo.seekWidgetByName(this.ccsNode, "Panel_chat" + i);
            Panel_chat.setVisible(false);
            this.mChatUIs.push(Panel_chat);
        }
    },

    //chat
    setChatMsg : function(userChatMsg){
        var seat = mo.dzpkTableStatus.getViewSeatForUid(userChatMsg.uid);
        var Panel_chat = this.mChatUIs[seat];
        Panel_chat.stopAllActions();

        var Image_chat = mo.seekWidgetByName(Panel_chat, "Image_chat");
        var Panel_audio = mo.seekWidgetByName(Image_chat, "Panel_audio");
        var Text_content = mo.seekWidgetByName(Image_chat, "Text_content");
        var Sprite_face = mo.seekWidgetByName(Panel_chat, "Sprite_face");

        Panel_chat.setVisible(false);
        Image_chat.setVisible(false);
        Sprite_face.setVisible(false);
        Panel_audio.setVisible(false);
        Text_content.setVisible(false);

        if (userChatMsg.type == 1){
            //处理文字消息
            var numIndex = parseInt(userChatMsg.msg);

            if (numIndex >=0 && numIndex < 13){
                //播放音效
                var sex = mo.dzpkTableStatus.getSexForUid(userChatMsg.uid);
                mo.soundHelper.playUserCustom(sex, numIndex);

                //快捷短语
                Text_content.setVisible(true);
                mo.gameHelper.setTextString(Text_content, mo.gameUserInfo.userCustomText[numIndex]);
                var sizeW = Text_content.getContentSize().width + 40;
                sizeW = Math.max(sizeW, 70);
                Text_content.setPositionX(sizeW / 2);

                Panel_chat.setVisible(true);
                Image_chat.setVisible(true);
                Image_chat.setContentSize(cc.size(sizeW, 64));

                Image_chat.runAction(
                    cc.sequence(
                        cc.delayTime(3.0),
                        cc.hide()
                    )
                );
            }else if (numIndex >= 51 && numIndex < 80){

                //表情
                Sprite_face.setVisible(true);
                var animation = new cc.Animation(mo.dzpkCardHelper.getFaceFrames(numIndex - 51));
                animation.setDelayPerUnit(0.2);
                Sprite_face.stopAllActions();
                Sprite_face.runAction(cc.repeatForever(cc.animate(animation)));

                Panel_chat.setVisible(true);

                Panel_chat.runAction(
                    cc.sequence(
                        cc.delayTime(3.0),
                        cc.hide(),
                        cc.callFunc(function(){
                            Sprite_face.stopAllActions();
                        }, Panel_chat)
                    )
                );
            }else{
                Panel_chat.setVisible(false);

                var formSeat = seat;
                var toSeat = mo.dzpkTableStatus.getViewForLogic(numIndex % 10, mo.dzpkTableStatus.mTableStatus.MySeat - 1);
                var index = (parseInt(numIndex / 10) % 10);
                var p1 = mo.dzpkTable.mDzpkUILayer.mPlayerPoss[formSeat];
                var p2 = mo.dzpkTable.mDzpkUILayer.mPlayerPoss[toSeat];

                mo.dzpkTable.mDzpkTablePlayerInfoLayer.showEffectChat(formSeat, toSeat, index, p1, p2);
            }
        }else{
            if (!cc.sys.isNative){
                return;
            }

            //语音
            Panel_audio.setVisible(true);

            Panel_chat.setVisible(true);
            Image_chat.setVisible(true);
            Image_chat.setContentSize(cc.size(70, 64));

            //语音信息
            var intArray = [];
            var vmsg = userChatMsg.vmsg;
            var lenght = vmsg.limit - vmsg.offset;
            for (var i = 0; i < lenght; i++){
                intArray.push(vmsg.view[i + vmsg.offset]);
            }

            var millisecond = mo.gameHelper.getMillisecond().toString();
            var fileName = millisecond + ".amr";
            mo.nativeHelper.playFile(fileName, intArray);

            var time = (lenght / 1500);
            Image_chat.runAction(
                cc.sequence(
                    cc.delayTime(time),
                    cc.hide()
                )
            );
        }
    },

})